Double hashing is a computer programming technique used in conjunction with open addressing in to resolve , by using a secondary hash of the key as an offset when a collision occurs. Double hashing with open addressing is a classical data structure on a table .
The double hashing technique uses one hash value as an index into the table and then repeatedly steps forward an interval until the desired value is located, an empty location is reached, or the entire table has been searched; but this interval is set by a second, independent hash function. Unlike the alternative collision-resolution methods of linear probing and quadratic probing, the interval depends on the data, so that values mapping to the same location have different bucket sequences; this minimizes repeated collisions and the effects of clustering.
Given two random, uniform, and independent hash functions and , the th location in the bucket sequence for value in a hash table of buckets is: Generally, and are selected from a set of universal hash functions; is selected to have a range of and to have a range of . Double hashing approximates a random distribution; more precisely, pair-wise independent hash functions yield a probability of that any pair of keys will follow the same bucket sequence.
In practice:
Let have fixed load factor . Bradford and Katehakis. showed the expected number of probes for an unsuccessful search in , still using these initially chosen hash functions, is regardless of the distribution of the inputs. Pair-wise independence of the hash functions suffices.
Like all other forms of open addressing, double hashing becomes linear as the hash table approaches maximum capacity. The usual heuristic is to limit the table loading to 75% of capacity. Eventually, rehashing to a larger size will be necessary, as with all other open addressing schemes.
There are additionally a significant number of mostly-overlapping hash sets; if and , then , and comparing additional hash values (expanding the range of ) is of no help.
&= h_1(y) + (k - i) (-h_2(x) - 2k h_3(x)) + (k-i)^2 h_3(x) \\ &= \ldots \\ &= h_1(x) + k h_2(x) + k^2 h_3(x) + (i - k) h_2(x) + (i^2 - k^2) h_3(x) \\ &= h_1(x) + i h_2(x) + i^2 h_3(x) \\ &= h(i, x). \\\end{align}
/// Calculate k hash values from two underlying hash functions /// h1() and h2() using enhanced double hashing. On return, /// hashesi = h1(x) + i*h2(x) + (i*i*i - i)/6. /// Takes advantage of automatic wrapping (modular reduction) /// of unsigned types in C. void ext_dbl_hash(struct key const *x, unsigned int hashes, unsigned int n) {
unsigned int a = h1(x), b = h2(x), i = 0;
hashes[i] = a; for (i = 1; i < n; i++) { a += b; // Add quadratic difference to get cubic b += i; // Add linear difference to get quadratic // i++ adds constant difference to get linear hashes[i] = a; }}
In addition to rectifying the collision problem, enhanced double hashing also removes double-hashing's numerical restrictions on 's properties, allowing a hash function similar in property to (but still independent of) to be used.
See also
External links
|
|